package com.roc.template.utils;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.stream.FileImageInputStream;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName Utils
 * @Description: TODO
 * @Author Administrator
 * @Date 2020/8/4 18 04
 * @Version V1.0
 **/
public class Utils {

    protected static Logger logger = LoggerFactory.getLogger(DateUtils.class);
    private static final int fontSize = 9;
    private static final int widthPercentage = 91;
    private static final int minimumHeight = 20;
    private static final int signatureHeight = 90;//像素
    private static final int signatureColWidth = 93;//像素
    private static final int signatureColNum = 4; //一行排几个签章
    private static final int cellPadding = 5; //一行排几个签章
    private static final String UNIT = "万千佰拾亿千佰拾万千佰拾元角分";
    private static final String DIGIT = "零壹贰叁肆伍陆柒捌玖";
    private static final double MAX_VALUE = 9999999999999.99D;

    private static Map<String, String> optionValueMap = new HashMap<>();
    private static Map<String, String> expressionMap=new HashMap<String, String>();
    public static final byte[] EMPTY_FILE = new byte[0];

    static {
        int resources = FontFactory.registerDirectory("resources");
        System.out.println(resources);
    }

    public static void main(String[] args) {
        printPdf();
    }


    public static String printPdf() {

        Document document = new Document(PageSize.A4, 0, 0, 10, 0);
        String pdfName =  "d://test.pdf";
        try {
            PdfWriter.getInstance(document, new FileOutputStream(pdfName));
            document.open();

            //收文模板
            formHead(document);// pdf头部
            split(document);// 表单内容


            //文件传阅单
            printRecords(document);// 审批记录
            feedbackList2(document); // 反馈记录
        }catch(Exception e ) {
            logger.error("pdf生成出错", e);
            e.printStackTrace();
        }finally {
            document.close();
        }
        return pdfName;
    }



    private static void feedbackList2(Document document) throws DocumentException {
        Font font = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        font.setSize(fontSize);

        PdfPTable table = createTable(document, 4);
        addCellToTable(table,font,"标题");
        addCellToTable(table,font,"title");
        addCellToTable(table,font,"提交人");
        addCellToTable(table,font,"万鹏程");
        document.add(table);

        table = createTable(document, 4);
        addCellToTable(table,font,"所属部门");
        addCellToTable(table,font,"开发部");
        addCellToTable(table,font,"原号");
        addCellToTable(table,font,"2131312");
        document.add(table);

        table = createTable(document, 4);
        addCellToTable(table,font,"流程提交日期");
        addCellToTable(table,font,"2020-08-08 22:22:22");
        addCellToTable(table,font,"收到日期");
        addCellToTable(table,font,"2020-08-08 22:22:22");
        document.add(table);


        table = createTable(document, 2);
        addCellToTable(table,font,"备注");
        addCellToTable(table,font,"remark");
        document.add(table);

        table = createTable(document, 2);
        addCellToTable(table,font,"文件");
        addCellToTable(table,font,"12.pdf");
        document.add(table);


        table = createTable(document, 4);
        addCellToTable(table,font,"编号");
        addCellToTable(table,font,"12121");
        addCellToTable(table,font,"文件标题");
        addCellToTable(table,font,"12");
        document.add(table);

        table = createTable(document, 4);
        addCellToTable(table,font,"原发日期");
        addCellToTable(table,font,"2020-08-08 22:22:22");
        addCellToTable(table,font,"发文单位");
        addCellToTable(table,font,"开发部");
        document.add(table);


        table = createTable(document, 4);
        addCellToTable(table,font,"份数");
        addCellToTable(table,font,"3");
        addCellToTable(table,font,"主要领导批示");
        addCellToTable(table,font,"同意");
        document.add(table);


        table = createTable(document, 2);
        addCellToTable(table,font,"分管领导批示");
        addCellToTable(table,font,"万鹏程，陈凯文");
        document.add(table);

    }

    private static void printRecords(Document document) throws DocumentException {

        Font font = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        font.setSize(18);
        PdfPTable table = new PdfPTable(3);
        table.setWidthPercentage(widthPercentage);
        logger.info("表格宽：{}",document.getPageSize().getWidth());
        table.setWidths(new float[] { 2, 3, 2 });
        PdfPCell cell = new PdfPCell(new Phrase(unicodeStr(""), font));
        cell.setBorder(0);
        cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        font.setStyle(Font.BOLD);
        cell = new PdfPCell(new Phrase(unicodeStr("文件传阅单"), font));
        cell.setBorder(0);
        cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(unicodeStr(""), font));
        cell.setBorder(0);
        cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        document.add(table);
    }

    private static PdfPTable createTable(Document document,Integer numColumns) throws DocumentException {
        float wight=document.getPageSize().getWidth();
        PdfPTable table = new PdfPTable(numColumns);
        table.setWidthPercentage(widthPercentage);
        int wcell=40;
        float w=(wight-numColumns*wcell)/numColumns;
        float[] per=new float[numColumns];
        for (int kk=0;kk<numColumns;kk++) {
            if(kk%2==0){
                per[kk]=wcell;
            }else{
                per[kk]=w;
            }
        }
        table.setWidths(per);
        return table;
    }

    private static void split(Document document) throws DocumentException, IOException {

        Font font = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        font.setSize(fontSize);

        PdfPTable table = createTable(document, 2);
        addCellToTable(table,font,"标题");
        addCellToTable(table,font,"title");
        document.add(table);


        table = createTable(document, 2);
        addCellToTable(table,font,"流水号");
        addCellToTable(table,font,"serialNo");
        document.add(table);

        table = createTable(document, 4);
        addCellToTable(table,font,"拟稿人");
        addCellToTable(table,font,"万鹏程");
        addCellToTable(table,font,"所属部门");
        addCellToTable(table,font,"开发部");
        document.add(table);



        table = createTable(document, 2);
        addCellToTable(table,font,"日期");
        addCellToTable(table,font,"2020-08-08 22:22:22");
        document.add(table);


        table = createTable(document, 4);
        addCellToTable(table,font,"建工交建字");
        addCellToTable(table,font,"2020-08-08 22:22:22");
        addCellToTable(table,font,"建工交建号");
        addCellToTable(table,font,"2132313131");
        document.add(table);


        table = createTable(document, 4);
        addCellToTable(table,font,"缓急");
        addCellToTable(table,font,"紧急");
        addCellToTable(table,font,"签发领导");
        addCellToTable(table,font,"万鹏程");
        document.add(table);


        table = createTable(document, 2);
        addCellToTable(table,font,"会签");
        addCellToTable(table,font,"万鹏程，陈凯文");
        document.add(table);


        table = createTable(document, 4);
        addCellToTable(table,font,"主送");
        addCellToTable(table,font,"万鹏程，陈凯文");
        addCellToTable(table,font,"抄送");
        addCellToTable(table,font,"万鹏程，陈凯文");
        document.add(table);


        table = createTable(document, 4);
        addCellToTable(table,font,"份数");
        addCellToTable(table,font,"2");
        addCellToTable(table,font,"正文");
        addCellToTable(table,font,"content");
        document.add(table);


        table = createTable(document, 2);
        addCellToTable(table,font,"主题词");
        addCellToTable(table,font,"2");
        document.add(table);


        table = createTable(document, 4);
        addCellToTable(table,font,"发起人");
        addCellToTable(table,font,"万鹏程，陈凯文");
        addCellToTable(table,font,"文件发布");
        addCellToTable(table,font,"万鹏程，陈凯文");
        document.add(table);


        table = createTable(document, 4);
        addCellToTable(table,font,"文件发布范围");
        addCellToTable(table,font,"万鹏程，陈凯文");
        addCellToTable(table,font,"归档目录");
        addCellToTable(table,font,"万鹏程");
        document.add(table);

    }

    //图片到byte数组
    public static byte[] image2byte(String path){
        byte[] data = null;
        FileImageInputStream input = null;
        try {
            input = new FileImageInputStream(new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int numBytesRead = 0;
            while ((numBytesRead = input.read(buf)) != -1) {
                output.write(buf, 0, numBytesRead);
            }
            data = output.toByteArray();
            output.close();
            input.close();
        }
        catch (FileNotFoundException ex1) {
            ex1.printStackTrace();
        }
        catch (IOException ex1) {
            ex1.printStackTrace();
        }
        return data;
    }

    private static void formHead(Document document) throws DocumentException, IOException {
        Font font = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        font.setSize(18);
        PdfPTable table = new PdfPTable(3);
        table.setWidthPercentage(widthPercentage);
        logger.info("表格宽：{}",document.getPageSize().getWidth());
        table.setWidths(new float[] { 2, 3, 2 });
        PdfPCell cell = new PdfPCell(new Phrase(unicodeStr(""), font));
        cell.setBorder(0);
        cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        font.setStyle(Font.BOLD);
        cell = new PdfPCell(new Phrase(unicodeStr("收文模板"), font));
        cell.setBorder(0);
        cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(unicodeStr(""), font));
        cell.setBorder(0);
        cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
        document.add(table);


//        font = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//        font.setSize(fontSize);
//        table = new PdfPTable(3);
//        table.setWidthPercentage(widthPercentage);
//        int top=9;
//        int bottom=5;
//        cell = new PdfPCell(new Phrase(unicodeStr("所属组织：客户应用体验圈"), font));
//        cell.setBorder(0);
//        cell.setPaddingTop(top);
//        cell.setPaddingBottom(bottom);
//        cell.setVerticalAlignment(Element.ALIGN_LEFT);
//        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
//        table.addCell(cell);
//        cell = new PdfPCell(new Phrase(
//                unicodeStr("打印时间: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())), font));
//        cell.setBorder(0);
//        cell.setVerticalAlignment(Element.ALIGN_CENTER);
//        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
//        cell.setPaddingTop(top);
//        cell.setPaddingBottom(bottom);
//        table.addCell(cell);
//        cell = new PdfPCell(new Phrase(unicodeStr("打印人: " + "万鹏程"), font));
//        cell.setBorder(0);
//        cell.setVerticalAlignment(Element.ALIGN_RIGHT);
//        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
//        cell.setPaddingTop(top);
//        cell.setPaddingBottom(bottom);
//        table.addCell(cell);
//        document.add(table);
    }

    /**
     * 重复代码太多了。。。
     *
     * @param table
     * @param font
     * @param cellContent
     */
    private static void addCellToTable(PdfPTable table, Font font, String cellContent) {
        PdfPCell cell = new PdfPCell(new Phrase(unicodeStr(cellContent), font));
        cell.setBorder(0);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setMinimumHeight(minimumHeight);
        table.addCell(cell);
    }


    private static String unicodeStr(String text) {

        if (StringUtils.isEmpty(text)) {
            return "";
        }
        try {
            return new String(text.getBytes("utf-8"));
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }

}
